var businessDelayChar;
var sySuccRatioChar;
var DNSServiceChar
var TCPServiceChar;
var SiteQuantityChar;
var downloadRatioChar;
var datatable;
var curTime = 'hour';
var granu = 'hour';
var param = new Object({
	init:function(){
		this.timeSize = ''
		this.beginDate = ''
		this.endDate = ''
		this.area = ''
		this.siteOption=''
	    this.sp=''
	    this.site=''
		this.customSite=''
	},
	setTimeParam:function(){
		this.beginDate = $('#' + granu + 'beginDate').val();
		this.endDate = $('#' + granu + 'endDate').val();
	},
	setSiteParam:function(siteOption){
		switch(siteOption) {
			case 'sp' :
				this.sp =$('#sp').val()
				this.site = $('#siteId').val();
				if ($('#latitude').is(':checked')) {
					this.area=$('#area').val()
				}
				break
			case 'site' :
				this.customSite=$('#customSite').val()
				break
		}
	},
	setParam:function(){
		this.init();
		this.timeSize = $('input:radio[name="timeSize"]:checked').val();
		this.area=$('#area').val();
		//this.beginDate = $('#beginDate').val();
		//this.endDate = $('#endDate').val();
		this.setTimeParam();
	    this.siteOption= $('input:radio[name="ab"]:checked').val();
		this.setSiteParam(this.siteOption)
//		this.sp = $('#sp').val();
//		this.site =$("#siteId").val();
	}
});

$(function() {
	//var sp = $("#sp").val();
	// loadSite(sp);
	 if($("#jspType").val() == 'kqiEvalution_zcsp'){
		 $("input[type='radio'][name=timeSize][value='"+$("#timeSize_link").val()+"']").attr("checked",true);
		 $("#sp").val($("#sp_name").val());
	}else if($("#jspType").val() == 'kqiEvalution_zczd'){
		$("input[type='radio'][name=timeSize][value='"+$("#timeSize_link").val()+"']").attr("checked",true);
		$("#sp").val($("#sp_name").val());  
	}
	 $("#sp").combobox({
		select : function(event, ui) {
			loadSite();
		},
		width : 100
	 });
	 
	 $("#siteId").combobox();
	 showArearList("area");
		
		$("#area").combobox({		
			width : 100
		 });
		
	 businessDelayChar = echarts.init(document.getElementById('businessDelayChar'));	 
	 sySuccRatioChar = echarts.init(document.getElementById('sySuccRatioChar'));	 
	 DNSServiceChar = echarts.init(document.getElementById('DNSServiceChar'));	 
	 TCPServiceChar = echarts.init(document.getElementById('TCPServiceChar'));	 
	 SiteQuantityChar = echarts.init(document.getElementById('SiteQuantityChar'));
	 downloadRatioChar = echarts.init(document.getElementById('downloadRatioChar'));

	 var timeSize = $('input[name="timeSize"]');
		timeSize.click(function() {
			granu = $(this).val();
			var granuDivId1 = granu + 'beginDate';
			var granuDivId2 = granu + 'endDate';
			var curTimeDivId1 = curTime + 'beginDate';
			var curTimeDivId2 = curTime + 'endDate';
			if (curTime == granu)
				return;
			$('#' + curTimeDivId1).hide();
			$('#' + curTimeDivId2).hide();
			$('#' + granuDivId1).show();
			$('#' + granuDivId2).show();
			curTime = granu;	
		});	
	 businessDelayChar.on('click', function(param) {
		 linkToHTTP(param.seriesName,param.name);
	 })	 
	 sySuccRatioChar.on('click', function(param) {
		 linkToHTTP(param.seriesName,param.name);
	 })
	 DNSServiceChar.on('click', function(param) {
		 linkToDNS(param.seriesName,param.name);
	 })	 
	 TCPServiceChar.on('click', function(param) {
		 linkToCOMM(param.seriesName,param.name);
	 })
	 SiteQuantityChar.on('click', function(param) {
		 linkToHTTP(param.seriesName,param.name);
	 })
	 downloadRatioChar.on('click', function(param) {
		 linkToHTTP(param.seriesName,param.name);
	 })

	 hideOrShow();
});

function resetQueryForm() {
	$('#queryForm')[0].reset();
	hideOrShow();
}

function getList(){
	
	param.setParam();
	beginDate=param.beginDate;
	endDate=param.endDate;
	var startSenconds = new Date(beginDate.replace("-", "/").replace("-", "/")).getTime();
	var endSenconds = new Date(endDate.replace("-", "/").replace("-", "/")).getTime();
	if(endSenconds <= startSenconds) {
		alert('结束时间必须大于开始时间！');
		return ;
	}
	
	var dimension = param.siteOption;	
    var siteV=$('#siteId').val();
    var domain = $('#customSite').val();
	if('site'==dimension && !domain){
		alert('请填写需要查询的域名！');
		return ;
	}
    if( 'site'==dimension || (''!= siteV&&siteV!=null)){   	
	    $('#spChar').hide();
		$('#siteDiag').show();
		getList4Site(); 
	 }else {
	   $('#spChar').show();
	   $('#siteDiag').hide();
       initSiteQuantityChar();
	 }
    initBusinessDelayChar();
	initSySuccRatioChar();
	initDNSServiceChar();
	initTCPServiceChar();
	initDownloadRatioChar();
}
// 业务时延
function initBusinessDelayChar(){
	//查询  
	businessDelayChar.clear();
	businessDelayChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../businessSense/spAnalysis/getBusinessDelay",
		dataType : "json",
		"data" : {
			"param" : JSON.stringify(param)
		},
		success : function(data) {
			businessDelayChar.setOption(data, true);
			businessDelayChar.hideLoading();
		}
	});
}

// 首页成功率
function initSySuccRatioChar(){
	//查询  
	sySuccRatioChar.clear();
	sySuccRatioChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../businessSense/spAnalysis/getSySuccRatio?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"param" : JSON.stringify(param)
		},
		success : function(data) {
			sySuccRatioChar.setOption(data, true);
			sySuccRatioChar.hideLoading();
		}
	});
}

// dns

function initDNSServiceChar(){
	//查询  
	DNSServiceChar.clear();
	DNSServiceChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../businessSense/spAnalysis/getDNSQuality?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"param" : JSON.stringify(param)
		},
		success : function(data) {
			DNSServiceChar.setOption(data, true);
			DNSServiceChar.hideLoading();
		}
	});
}
// TCP 服务质量

function initTCPServiceChar(){
	//查询  
	TCPServiceChar.clear();
	TCPServiceChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../businessSense/spAnalysis/getTCPService?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"param" : JSON.stringify(param)
		},
		success : function(data) {
			TCPServiceChar.setOption(data, true);
			TCPServiceChar.hideLoading();
		}
	});
}

//站点质量
function initSiteQuantityChar(){
	//查询  
	SiteQuantityChar.clear();
	SiteQuantityChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../businessSense/spAnalysis/getSiteQuantity?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"param" : JSON.stringify(param)
		},
		success : function(data) {
			SiteQuantityChar.setOption(data, true);
			SiteQuantityChar.hideLoading();
		}
	});
}

//选择站点时 显示 关于 站点的图表

function getList4Site() {
	
	//alert(JSON.stringify(param))

	$("#table tbody tr").remove();
	if (datatable == undefined || datatable == "undefined" || datatable == null) {
	} else {
		datatable.destroy();
	}
	datatable = $('#table').DataTable(
			{
				"aoColumnDefs" : [ {
					sDefaultContent : '',
					aTargets : [ '_all' ]
				} ],
				"bPaginate" : false, // 翻页功能
				"bLengthChange" : false, // 改变每页显示数据数量
				"bFilter" : false, // 过滤功能
				"bSort" : true, // 排序功能
				"bInfo" : false,// 页脚信息
				"bAutoWidth" : true,// 自动宽度
				"aaSorting":[5,'desc'],
				// "sPaginationType": "full_numbers",//显示分页的样式
				// 还有simple_numbers等
//				"sScrollX" : "100%",
				"sScrollXInner" : "110%",
				"bScrollCollapse" : true,
				"oLanguage" : {
					"sLengthMenu" : "每页显示 _MENU_条",
					"sZeroRecords" : "没有找到符合条件的数据",
					"sProcessing" : "&nbsp;&nbsp;&nbsp;&nbsp;查询中，请稍等...",
					"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
					"sInfoEmpty" : "没有记录",
					"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
					"sSearch" : "搜索：",
					"oPaginate" : {
						"sFirst" : "首页",
						"sPrevious" : "前一页",
						"sNext" : "后一页",
						"sLast" : "尾页"
					}
				},
				"bProcessing" : true,
				"bServerSide" : true,
				"sAjaxSource" : "../../businessSense/spAnalysis/getList4Site",
				"fnServerData" : function(sSource, aoData, fnCallback) {
					$.ajax({
						"type" : "post",
						"contentType" : "application/x-www-form-urlencoded",
						"url" : sSource,
						"dataType" : "json",
						"data" : {
							"aoData" : JSON.stringify(aoData),
							"param" : JSON.stringify(param)
						}, // 以json格式传递
						"success" : function(resp) {
							fnCallback(resp.allData);
						}
					});
				},
		
				columns : [ {
					data : "at_date"
				}, {
					data : "domainname"
				}, {
					data : "ip"
				}, {
					data : "request_num"
				}, {
					data : "httpsuccratio"
				}, {
					data : "tcp_ratio"
				}, {
					data : "frist_page_time_delay"
				}, {
					data : "frist_pack_time_delay"
				} ]
		
			});
  }

//下载速率
function initDownloadRatioChar(){
	//查询  
	downloadRatioChar.clear();
	downloadRatioChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../businessSense/spAnalysis/getDownloadRatio?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"param" : JSON.stringify(param)
		},
		success : function(data) {
			downloadRatioChar.setOption(data, true);
			downloadRatioChar.hideLoading();
		}
	});
}

function loadSite(sp) {
	var sp = $("#sp").val();
	$.ajax({
				url : '../../businessSense/spAnalysis/getSiteInfos?sp='+ sp,
				type : 'POST',
				dataType : 'JSON',
				error : function() {
					alert('Error loading data!');
				},
				success : function(msg) {
					$("#siteId").remove();
					$("#siteId").empty();
					$("#siteTD")
							.append(
									$("<select id='siteId' name='siteId'></select>"));
					
					$("#siteId").append("<option value=''>全部</option>");
					var list = msg.siteList;
					$.each(eval(list), function(i, item) {
						$(
								"<option value='" + item.site + "'>"
										+ item.site + "</option>")
								.appendTo($("#siteId"));
					});
					if($("#jspType").val() == 'kqiEvalution_zczd'){
						//$("#siteId option[value="+$("#site_name").val()+"]").attr("selected", true);
						$("#siteId").val($("#site_name").val());
					}
					$("#siteId").combobox();
				}
			});
}

//是否选择  域名 展示的 查询维度
function hideOrShow(){
	var siteOption= $('input:radio[name="ab"]:checked').val();
	if(siteOption == 'site'){
		$("#radio_site").show();
		$("#radio_sp").hide();
	}else if(siteOption == 'sp') {
		$("#radio_sp").show();
		$("#radio_site").hide();
	}else{
		$("#radio_sp").hide();
		$("#radio_site").hide();
	}
}


//跳转

function linkToDNS(name,eCharsTime) {
	var p = "timeSize=" + param.timeSize + "&beginDate=" + param.beginDate + "&endDate="
	+ param.endDate + "&city_code=" + param.area 
	p += "&queryTime="+eCharsTime+"&jspType=SPAnalysis"
	url = "/businessAnalysis/DNSAnalysis/index?" + p;	
	title = "DNS指标"
	linkTab(title, url)
}

function linkToTCP(name,eCharsTime) {
	var p = "timeSize=" + param.timeSize + "&beginDate=" + param.beginDate + "&endDate="
	+ param.endDate + "&city_code=" + param.area 
	p += "&queryTime="+eCharsTime+"&jspType=SPAnalysis"
	url = "/businessAnalysis/TCPAnalysis/index?" + p;	
	title = "TCP性能分析"
	linkTab(title, url)
}

function linkToHTTP(name,eCharsTime) {
	var p = "timeSize=" + param.timeSize + "&beginDate=" + param.beginDate + "&endDate="
	+ param.endDate + "&city_code=" + param.area 
	p += "&queryTime="+eCharsTime+"&jspType=SPAnalysis"
	url = "/businessAnalysis/hcq/index?" + p;	
	title = "HTTP过程指标"
	linkTab(title, url)
}

function linkToCOMM(name,eCharsTime) {
	var p = "timeSize=" + param.timeSize + "&beginDate=" + param.beginDate + "&endDate="
	+ param.endDate + "&city_code=" + param.area 
	p += "&queryTime="+eCharsTime+"&jspType=SPAnalysis"
	url = "/businessAnalysis/commonBusinessAnalysis/index?" + p;	
	title = "通用业务性能"
	linkTab(title, url)
}

function forwordSP() {
	var p = "timeSize=" + param.timeSize + "&beginDate=" + param.beginDate + "&endDate="
	+ param.endDate + "&city_code=" + param.area 
	p += "&siteOption="+param.siteOption+"&sp=" + param.sp +"&site=" + param.site +"&customSite=" + param.customSite
	p += "&jspType=SPAnalysis_zc"
	url = "/businessAnalysis/sp/index?" + p;	
	title = "SP性能分析"
	linkTab(title, url)
}





